package pt.cp.model;

import model.FavoritableEntity;
import model.KeyEntity;

import java.io.IOException;
import java.net.URL;
import java.util.Scanner;

public class Stations extends FavoritableEntity<String, Station> {
    public static final String STATIONS_URL = "http://www.cp.pt/cp/getStations.do";

    public KeyEntity<String, Station> getMostUsed() {
        return mostUsed;
    }

    public final KeyEntity<String, Station> mostUsed = newInstance();

    public Stations() {
        super(Station[].class, Station.class);
    }

    @Override
    protected void parse() {

        try {
            URL url = new URL(STATIONS_URL);
            try (Scanner scanner = new Scanner(url.openStream())) {
                final String[] strings = scanner.nextLine().split(";");
                for (String s : strings)
                    add(new Station(s));
            }
        } catch (IOException e1) {
            throw new RuntimeException(e1);
        }
    }
}
